﻿using System.Linq;
using DataScripter.Excel.Formulas;

namespace DataScripter.Excel.Functions
{
	internal abstract class Function : Formula
	{
		private readonly string _formulaName;
		private readonly Formula[] _parameters;

		public Function(string formulaName, params Formula[] parameters)
		{
			_formulaName = formulaName;
			_parameters = parameters;
		}

		#region Overrides of Formula

		public override string Resolve(ICell current)
		{
			string[] resolvedParameters = new string[_parameters.Length];
			for (int i = 0; i < _parameters.Length; i++)
			{
				resolvedParameters[i] = _parameters[i].Resolve(current);
			}
			return _formulaName + "(" + string.Join(";", resolvedParameters) + ")";
		}

		#endregion

		public override string ToString()
		{
			return _formulaName + "(" + string.Join(",", _parameters.Select(p => p.ToString()).ToArray()) + ")";
		}
	}
}